package de.hft.st.algodat.ctt;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import de.hft.st.algodat.ctt.algo.EA;
import de.hft.st.algodat.ctt.algo.MyopicConstruction;
import de.hft.st.algodat.ctt.algo.ReactiveConstruction;
import de.hft.st.algodat.ctt.io.DataIO;
import de.hft.st.algodat.ctt.model.Problem;
import de.hft.st.algodat.ctt.model.Solution;

public class CTTApplication {
	private final boolean DEBUG = false;
	private Problem theProblem;
	private DataIO dataIO;
	private Solution theSolution;
	private ReactiveConstruction geiger;
	private MyopicConstruction myopic;
	private EA ea;

	protected CTTApplication() {
		// // pretend loading triggered
		// for(int i = 1; i <= 21; i++) {
		// System.out.printf("loading instance #%02d ...",i);
		// this.theProblem = new Problem();
		// this.dataIO = new DataIO();
		// dataIO.readProblemInstanceFromXML(String.format("comp%02d.xml",i),
		// theProblem);
		// System.out.printf("completed.\n");
		// }
		// System.out.println("all loaded");
		// pretend loading triggered

		String pInstance = "comp01";

		log("loading instance " + pInstance);
		this.theProblem = new Problem();
		this.dataIO = new DataIO();
		dataIO.readProblemInstanceFromXML(String.format(pInstance), theProblem);
		log("IN SUM " + theProblem.getNumberOfCoursesInCurricula()
				+ " lectures.");

		this.ea = new EA(theProblem.getNumberOfCoursesInCurricula(), theProblem
				.getNumberOfTimeslots());

		long start = System.currentTimeMillis();
		TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");

		for (int i = 0; i < 5; i++) {
			theSolution = new Solution(theProblem.getDays(), theProblem
					.getPeriods_per_day(), theProblem.getRooms());

			myopic = new MyopicConstruction(theProblem, theSolution, this.ea);
			myopic.constructFeasibleSolution();
			dataIO.saveFile(pInstance, theSolution.toString());
			long intermediate = System.currentTimeMillis();
			System.out.println(sdf.format(intermediate - start));
		}
		long end = System.currentTimeMillis();
		System.out.println(sdf.format(end - start));
		//this.dumpProblemData();
		dataIO.saveFile(pInstance, theSolution.toString());
		System.exit(0);
	}

	private void log(String msg) {
		if (DEBUG) {
			System.out.println(msg);
		}
	}

	private void dumpProblemData() {
		System.out.println(this.theProblem.toString());
	}
}
